#ifndef _MAP_C
#define _MAP_C

//includes
#include "map.h"

//functions that run the map movement and or location
//Init map
void init_map(void){
    int i,j;
    for(i=0;i<46;i++){
        tile[i]=create_bitmap(TILE_SIZE,TILE_SIZE);
        masked_blit((BITMAP *)maptiles[i].dat,tile[i],0,0,0,0,TILE_SIZE,TILE_SIZE);
    }
    for(i=0;i<MAP_H;i++){
        for(j=0;j<MAP_W;j++){
            //tiles[i][j].bmp=create_bitmap(TILE_SIZE,TILE_SIZE);
            //clear_bitmap(tiles[i][j].bmp);
            //if(map[i][j]!=0)masked_blit(tile[map[i][j]],tile[tiles[i][j].bmp],0,0,0,0,TILE_SIZE,TILE_SIZE);
            tiles[i][j].bmp=0;
            tiles[i][j].y1=i*TILE_SIZE;
            tiles[i][j].y2=i*TILE_SIZE+TILE_SIZE;
            tiles[i][j].x1=j*TILE_SIZE;
            tiles[i][j].x2=j*TILE_SIZE+TILE_SIZE;
            tiles[i][j].att=0;      //Not Collidable by default Need to SET
            tiles[i][j].mov=0;      //Not Moveable by default
            tiles[i][j].energy=0;   //default not set either
        }
    }
}    

//draw map to secondary buffer
void draw_map(short map_x,short map_y){
    short i,j;
    for(i=cam_y;i<cam_yoff;i++){//MAP_H;i++){
             for(j=cam_x;j<cam_xoff;j++){//MAP_W;j++){
                //if(tiles[i][j].bmp!=0){
                  masked_blit(tile[tiles[i][j].bmp],buffer,0,0,tiles[i][j].x1,tiles[i][j].y1,TILE_SIZE,TILE_SIZE);
             }
    }       
}

void destroy_tiles(void){
    int i;
    for(i=0;i<46;i++){
        destroy_bitmap(tile[i]);
    }
}    

void move_camera(int dir){
    switch (dir){
        case LEFT:
            cam_x--;cam_xoff--;
            if(cam_x<0)cam_x=0;
            if(cam_xoff<EDIT_MAP_SIZE_W/TILE_SIZE)cam_xoff=EDIT_MAP_SIZE_W/TILE_SIZE;
            break;
        case RIGHT:
            cam_x++;cam_xoff++;
            if(cam_x>MAP_W-TILE_SIZE)cam_x=MAP_W-TILE_SIZE;
            if(cam_xoff>MAP_W)cam_xoff=MAP_W;
            break;
        case UP:
            cam_y--;cam_yoff--;
            if(cam_y<0)cam_y=0;
            if(cam_yoff<EDIT_MAP_SIZE_H/TILE_SIZE)cam_yoff=EDIT_MAP_SIZE_H/TILE_SIZE;
            break;
        case DOWN:
            cam_y++;cam_yoff++;
            if(cam_y>MAP_H-TILE_SIZE)cam_y=MAP_H-TILE_SIZE;
            if(cam_yoff>MAP_H)cam_yoff=MAP_H;
            break;
    }    
}    

int save_map(BITMAP *back){
    int i,j,ch,done=0,no_save=0;
    char temp_name[5]={0,0,0,0,0},
        char_buf[2]={0,0};
     
    BITMAP *buff;
    buff=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    clear_keybuf();
    while(!done){
         blit(back,buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
         textprintf_centre_ex(buff,font,320,250,makecol(rand()%255,rand()%255,rand()%255),-1,"M A P N A M E");
         textprintf_centre_ex(buff,font,320,300,makecol(255,255,255),-1,"%s",(char *)temp_name);
         draw_buffer(buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
         ch = readkey();          
         if((ch&0xff)>='A'&&(ch&0xff)<='Z'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }
         else if((ch&0xff)>='a'&&(ch&0xff)<='z'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }   
         else if((ch&0xff)>='0'&&(ch&0xff)<='9'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }  
         else if((ch&0xff)=='.'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }   
         else if((ch&0xff)==27){
             no_save=1;done=1;keyhold[KEY_ESC]=1;
         }     
         else if((ch&0xff)==8){
             keyhold[KEY_BACKSPACE]=1;
             temp_name[strlen(temp_name)-1]=0;
         } 
         else if((ch&0xff)==13){
              keyhold[KEY_ENTER]=1;
              done=1;
         }    
    }  
    destroy_bitmap(buff);  
    if(!no_save){
     PACKFILE *pfile;
     pfile= pack_fopen(temp_name,"wp");
     if(!pfile){
         strcpy(cur_map_name,"NO FILE");
         return -1;
     }
     for(i=0;i<MAP_H;i++){
         for(j=0;j<MAP_W;j++){
             pack_iputl(tiles[i][j].bmp, pfile);
             pack_iputl(tiles[i][j].x1, pfile);
             pack_iputl(tiles[i][j].y1, pfile);
             pack_iputl(tiles[i][j].x2, pfile);
             pack_iputl(tiles[i][j].y2, pfile);
             pack_iputl(tiles[i][j].att, pfile);
             pack_iputl(tiles[i][j].mov, pfile);
             pack_iputl(tiles[i][j].energy, pfile);
         }
     }
     strcpy(cur_map_name,temp_name);
     pack_fclose(pfile);
    }    
    return 0;
}      
    
int load_map(BITMAP *back){
    int i,j,ch,done=0,no_load=0;
    char temp_name[5]={0,0,0,0,0},
        char_buf[2]={0,0};
     
    BITMAP *buff;
    buff=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    clear_keybuf();
    while(!done){
         blit(back,buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
         textprintf_centre_ex(buff,font,320,250,makecol(rand()%255,rand()%255,rand()%255),-1,"M A P N A M E");
         textprintf_centre_ex(buff,font,320,300,makecol(255,255,255),-1,"%s",(char *)temp_name);
         draw_buffer(buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
         ch = readkey();          
         if((ch&0xff)>='A'&&(ch&0xff)<='Z'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }
         else if((ch&0xff)>='a'&&(ch&0xff)<='z'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }    
         else if((ch&0xff)>='0'&&(ch&0xff)<='9'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }  
         else if((ch&0xff)=='.'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }      
         else if((ch&0xff)==27){
             no_load=1;done=1;keyhold[KEY_ESC]=1;
         } 
         else if((ch&0xff)==8){
             keyhold[KEY_BACKSPACE]=1;
             temp_name[strlen(temp_name)-1]=0;
         } 
         else if((ch&0xff)==13){
              keyhold[KEY_ENTER]=1;
              done=1;
         }    
    }  
    destroy_bitmap(buff);  
    if(!no_load){ 
     PACKFILE *pfile;
     pfile= pack_fopen(temp_name,"rp");
     if(!pfile){
         strcpy(cur_map_name,"NO FILE");
         return -1;
     }    
     for(i=0;i<MAP_H;i++){
         for(j=0;j<MAP_W;j++){
             tiles[i][j].bmp=pack_igetl(pfile);
             tiles[i][j].x1=pack_igetl(pfile);
             tiles[i][j].y1=pack_igetl(pfile);
             tiles[i][j].x2=pack_igetl(pfile);
             tiles[i][j].y2=pack_igetl(pfile);
             tiles[i][j].att=pack_igetl(pfile);
             tiles[i][j].mov=pack_igetl(pfile);
             tiles[i][j].energy=pack_igetl(pfile);
         }
     }
     strcpy(cur_map_name,temp_name);
     pack_fclose(pfile);
    }     
    return 0;
}            

#endif
